package am.NotenProgramm.Frame.Panels;
import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridLayout;

import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.ScrollPaneConstants;
import javax.swing.table.DefaultTableCellRenderer;

import am.NotenProgramm.Frame.GradePro;


public class StatisticsPanel extends JPanel{

	JScrollPane listPane;
	JTable listSubjects;
	JPanel tablePanel;
	
	double compPoints = 0.0;
	double avGrade = 0.0;
	double minPoints = 0.0;
	double plusPoints = 0.0;
	
	String[] tableHeader = {GradePro.rb.getString("tableName"),GradePro.rb.getString("testAverage"),GradePro.rb.getString("tableIsOral"),GradePro.rb.getString("oralWeight"),GradePro.rb.getString("subjectWeight"),GradePro.rb.getString("tableGrade"),GradePro.rb.getString("finalGrade"),GradePro.rb.getString("compensationPoints")};
	Object[][] tableContent;
			
	public StatisticsPanel(Dimension size) {
		setSize(size);
		initUI();
	}
	
	public void initUI() {
		setLayout(null);
		
		listPane = new JScrollPane();
		tablePanel = new JPanel();
		
		tablePanel.setSize((int)(getWidth()-40), (int)(getHeight()*0.9));
		tablePanel.setLocation(20, 20);
		tablePanel.setLayout(new GridLayout(1, 1));
		
		refresh();
		
		add(tablePanel);		
	}
	
	public void refresh() {
		tablePanel.setVisible(false);
		tablePanel.removeAll();
		listPane = new JScrollPane();
		
		String subjectStrings[] = new String[GradePro.myProfile.getSubjects().length+1];
		for (int i = 0; i < subjectStrings.length - 1; i++) {
			subjectStrings[i] = GradePro.myProfile.getSubjects()[i].getName();
		}
		
		tableContent = new Object[GradePro.myProfile.getSubjects().length+1][8];
		for(int i = 0; i < GradePro.myProfile.getSubjects().length; i++){
			tableContent[i][0] = GradePro.myProfile.getSubjects()[i].getName();
			tableContent[i][1] = Math.round(GradePro.myProfile.getSubjects()[i].getAverageTests()*1000)/1000.0;
			tableContent[i][2] = Math.round(GradePro.myProfile.getSubjects()[i].getAverageOral()*1000)/1000.0;
			tableContent[i][3] = Math.round(GradePro.myProfile.getSubjects()[i].getOralWeight()*1000)/1000.0;
			tableContent[i][4] = Math.round(GradePro.myProfile.getSubjects()[i].getSubjectWeight()*1000)/1000.0;
			tableContent[i][5] = Math.round(GradePro.myProfile.getSubjects()[i].getAverage()*1000)/1000.0;
			tableContent[i][6] = Math.round(2 * (GradePro.myProfile.getSubjects()[i].getAverage())) / 2.0;
			if (0 <= GradePro.myProfile.getSubjects()[i].getAverage() - 4) {
				tableContent[i][7] = GradePro.myProfile.getSubjects()[i].getSubjectWeight() * 
						Math.round(2 * (GradePro.myProfile.getSubjects()[i].getAverage() - 4)) / 2.0;
			} else {
				tableContent[i][7] = GradePro.myProfile.getSubjects()[i].getSubjectWeight() * 
						Math.round(2 * (GradePro.myProfile.getSubjects()[i].getAverage() - 4));
			}
		}
		// Durchschnitt anzeigen
		tableContent[GradePro.myProfile.getSubjects().length][0] = "Durchschnitt";
		tableContent[GradePro.myProfile.getSubjects().length][1] = "";
		tableContent[GradePro.myProfile.getSubjects().length][2] = "";
		tableContent[GradePro.myProfile.getSubjects().length][3] = "";
		tableContent[GradePro.myProfile.getSubjects().length][4] = "";
		
		refreshAverage();
		tableContent[GradePro.myProfile.getSubjects().length][5] = avGrade;
		tableContent[GradePro.myProfile.getSubjects().length][6] = "";
		tableContent[GradePro.myProfile.getSubjects().length][7] = compPoints;
		
		listSubjects = new JTable(tableContent, tableHeader);
		listSubjects.getColumnModel().getColumn(7).setCellRenderer(new CustomRenderer());
		listSubjects.getColumnModel().getColumn(0).setPreferredWidth((getWidth()-40)/4);
		listSubjects.setEnabled(false);
		listSubjects.setFont(new Font(Font.DIALOG, Font.PLAIN, 18));
		listSubjects.setRowHeight(22);
		listPane = new JScrollPane(listSubjects,ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
		tablePanel.add(listPane);
		tablePanel.setVisible(true);
	}
	
	public void refreshAverage() {
		// Calculate Averages and Compensation Points
		double tmpAverage = 0.0;
		minPoints = 0.0;
		plusPoints = 0.0;
		int tmpDivisor = 0;
		for (int i = 0; i < GradePro.myProfile.getSubjects().length; i++) {
			if(GradePro.myProfile.getSubjects()[i].getAverage() != 0.0){	
				tmpDivisor += GradePro.myProfile.getSubjects()[i].getSubjectWeight();
				tmpAverage += GradePro.myProfile.getSubjects()[i].getAverage()*GradePro.myProfile.getSubjects()[i].getSubjectWeight();
				if (0 <= GradePro.myProfile.getSubjects()[i].getAverage() - 4) {
					plusPoints += GradePro.myProfile.getSubjects()[i].getSubjectWeight()*(Math.round(2 * (GradePro.myProfile.getSubjects()[i]
							.getAverage() - 4)) / 2.0);
				} else {
					minPoints += GradePro.myProfile.getSubjects()[i].getSubjectWeight()*(Math.abs(Math.round(2 * (GradePro.myProfile
							.getSubjects()[i].getAverage() - 4))));
				}
			}
		}
		compPoints = plusPoints - minPoints;
		minPoints = minPoints / 2;
		avGrade = Math.round(1000 * tmpAverage / tmpDivisor) / 1000.0;
		
	}
}

class CustomRenderer extends DefaultTableCellRenderer {
private static final long serialVersionUID = 6703872492730589499L;
    public Component getTableCellRendererComponent(JTable table, Object value, boolean isSelected, boolean hasFocus, int row, int column)
    {
        Component cellComponent = super.getTableCellRendererComponent(table, value, isSelected, hasFocus, row, column);

        if(Double.parseDouble(table.getValueAt(row, column).toString()) >= 0){
            cellComponent.setBackground(Color.GREEN);
        } else{
            cellComponent.setBackground(Color.RED);
        }
        return cellComponent;
    }
}
